﻿using System.Collections.Generic;
using System.Linq;
using ExBlogger.Domain;
using ExBlogger.Domain.Repositories;
using NHibernate.Linq;
using NHibernate.Transform;
using SharpArch.NHibernate;

namespace ExBlogger.Repository {
    public class UsersRepository : NHibernateRepository<User>, IUsersRepository {

        public IList<User> GetUsersByCriteria() {
            return Session.CreateCriteria<User>().List<User>();
        }

        public IList<User> GetUsersByQueryOver() {
            return Session.QueryOver<User>().List<User>();
        }

        public IList<User> GetUsersByHQL() {
            return Session.CreateQuery("from User").List<User>();
        }

        public IList<User> GetUsersBySQL() {
            return Session.CreateSQLQuery("select * from Users")
                .SetResultTransformer(Transformers.AliasToBean<User>())
                .List<User>();
        }


        public IList<User> GetUsersByLinq() {
            return Session.Query<User>().ToList<User>();
        }

        public override User Save(User entity)
        {
            using (var tx = Session.BeginTransaction())
            {
                base.Save(entity);
                tx.Commit();
            }
            return entity;
        }
    }
}
